package edu.utexas.esel.jgraphpad.archpadplugin.graph;

import java.awt.Component;

import javax.swing.AbstractCellEditor;

import org.jgraph.JGraph;
import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.GraphCellEditor;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;

import com.jgraph.pad.graph.JGraphpadVertexView;

import edu.utexas.esel.jgraphpad.archpadplugin.Utils;
import edu.utexas.esel.jgraphpad.archpadplugin.model.Element;
import edu.utexas.esel.jgraphpad.archpadplugin.ui.ElementEditor;

/**
 * This class represents a {@link VertexView} for vertices in an ArchPad
 * model.
 *
 * @author svetoslavganov@mail.utexaxs.edu
 *
 */
public class ArchPadVertexView extends JGraphpadVertexView {

	private static final long serialVersionUID = 1L;

	private static VertexRenderer sRenderer = new ArchPadVertexRenderer();

	private GraphVertexEditor mVertexEditor;

	/**
	 * Empty constructor for persistence.
	 */
	public ArchPadVertexView() {
		this(null);
	}

	/**
	 * Constructs a new vertex view for the specified cell.
	 *
	 * @param cell The cell to construct the vertex view for.
	 */
	public ArchPadVertexView(Object cell) {
		super(cell);
	}

	@Override
	public GraphCellEditor getEditor() {
		if (mVertexEditor == null) {
			mVertexEditor = new GraphVertexEditor();
		}
		return mVertexEditor;
	}

	@Override
	public CellViewRenderer getRenderer() {
		return sRenderer;
	}

	/**
	 * This class is editor component for cells that contain {@link Element}.
	 */
	private static class GraphVertexEditor extends AbstractCellEditor
	        implements GraphCellEditor {

		private static final long serialVersionUID = 1L;

		private ElementEditor mEditor = new ElementEditor();

		@Override
		public Object getCellEditorValue() {
			return mEditor.getCurrentValue();
		}

		@Override
		public Component getGraphCellEditorComponent(JGraph graph, Object cell,
				boolean isSelected) {
			Element element = Utils.getCellElement(cell);
			mEditor.initialize(this, element) ;
			mEditor.setVisible(true);
			return mEditor;
		}
	}
}
